<?php

namespace app\admin\controller\project;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Docadmin extends Base
{
    protected $beforeActionList = [
        'first',
        'second' => ['only' => 'docdelete,doceditform'],
        'three' => ['only' => 'docdelete,doceditform']

    ];
    protected $project_user = [];
    protected $doc_type = [];

    protected function first()
    {
        $post = $this->request->param();
        $info = Db::name('project_user')
            ->where('project_id', $post['project_id'])
            ->where('uid', session('uid'))
            ->find();
        //dump($info);
        if (!$info) {
            $this->error('你没有权限进入本模块');
        } else {
            $this->assign('doc_type', config('param.doc_type'));
            $this->doc_type = config('param.doc_type');
            $this->project_user = $info;
            $this->assign('project_id', $post['project_id']);
        }
    }

    protected function second()
    {
        $info = $this->project_user;

        if ($info['user_type'] > 1) {
            $this->error('当前用户类型不能进行此操作');
        }
    }

    protected function three()
    {
        $post = $this->request->param();
        $docinfo = Db::name('doc')->where(['id' => $post['id']])->find();
        if ($docinfo['doc_view'] == 1 && ($docinfo['uid'] != session('uid'))) {
            $this->error('不是文档所有者不能查看或修改!');
        }
    }


    public function wx_msg($info, $stories_id, $model_status = 0)
    {
        $post = $this->request->param();
        $data['project_id'] = $post['project_id'];
        $data['model_id'] = 4;//文档
        $data['info'] = $info;
        $data['stories_id'] = $stories_id;//文档 ID
        $data['model_status'] = 0;
        $data['c_time']=time();
        $data['uid']=session('uid');
        DB::name('wx_msg')->insert($data);
    }


    public function index()
    {
        return view();
    }


    public function getcommentcount()
    {
        $data['data'] = Db::name('doccomment')
            ->where('doc_id', input('param.doc_id'))
            ->count();
        if ($data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function doc_comment_list()
    {
        $data = array();
        $post = $this->request->param();
        unset($post['page']);
        unset($post['limit']);
        unset($post['keyword']);
        $data['count'] = DB::view('doccomment', 'id,uid,c_time,info,file_path')
            ->view('admin', 'nickname', 'doccomment.uid=admin.id')
            ->where('doc_id', $post['doc_id'])
            ->count();
        $data['data'] = DB::view('doccomment', 'id,uid,c_time,info,file_path')
            ->view('admin', 'nickname', 'doccomment.uid=admin.id')
            ->where('doc_id', $post['doc_id'])
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();

        foreach ($data['data'] as &$item) {
            $item['c_time'] = date('Y-m-d  H:i:s', $item['c_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function doclists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $map[] = ['doc.delete_time', '=', 0];
        if (empty($post['title']) || $post['title'] == '') {
        } else {
            $map[] = ['doc.title', 'like', '%' . $post['title'] . '%'];
        }
        if (empty($post['status']) || $post['status'] == '') {

        } else {
            $map[] = ['doc.doc_type', '=', $post['status']];
        }
        $map[] = ['doc.project_id', '=', $post['project_id']];
        $data['count'] = Db::view('doc', 'id,project_id,title,c_time,uid,doc_type,doc_view,info_id')
            ->view('admin', 'nickname', 'doc.uid=admin.id', 'LEFT')
            ->where($map)
            ->count();
        $data['data'] = Db::view('doc', 'id,project_id,title,c_time,uid,doc_type,doc_view,info_id')
            ->view('admin', 'nickname', 'doc.uid=admin.id', 'LEFT')
            ->where($map)
            // ->fetchSql(true)
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();
        $doc_type = config('param.doc_type');
        $doc_view = config('param.doc_view');
        foreach ($data['data'] as &$info) {
            $info['doc_type'] = $doc_type[$info['doc_type']];
            $info['doc_view'] = $doc_view[$info['doc_view']];
            $info['c_time'] == 0 ? $info['c_time'] = '--' : $info['c_time'] = date('Y-m-d', $info['c_time']);
        }

        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function addcomment()
    {
        if ($this->request->isPost()) {

            Db::startTrans();
            try {
                $post = $this->request->param();
                unset($post['file']);
                $post['c_time'] = time();
                $post['uid'] = session('uid');
                $post['info'] = $this->request->param('info', '', null);
                Db::name('doccomment')->strict(false)->insert($post);
                $this->wx_msg('文档增加讨论', $post['doc_id']);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }


    public function docadd()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $validate = new \app\admin\validate\Doc();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            Db::startTrans();
            try {
                $post['info_id'] = Db::name('infoview')->insertGetId(['info' => $this->request->param('info', '', null)]);
                unset($post['info']);
                $post['c_time'] = time();
                $post['uid'] = session('uid');
                unset($post['id']);
                unset($post['editormd-html-code']);
                $post['doc_id'] = Db::name('doc')->strict(false)->insertGetId($post);
                $this->wx_msg('增加新的文档', $post['doc_id']);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        } else {
            $this->assign('info', ['project_id' => input('param.project_id')]);
            $this->assign('doc_type', config('param.doc_type'));
            $this->assign('doc_view', config('param.doc_view'));
            return view();
        }
    }

    public function docdelete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }

            Db::startTrans();
            try {
                 Db::name('doc')->where(['id' => $post['id']])->useSoftDelete('delete_time', time())->delete();
                $this->wx_msg('文档删除', $post['id']);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }


    public function docwork()
    {

        $post = $this->request->param();
        if ($this->request->isPost()) {
            //单个字段修改
            if (count($post) == 3) {

                Db::startTrans();
                try {
                    Db::name('project')->where(['id' => $post['id']])->update($post);
                    $this->wx_msg('文档编辑操作', $post['id']);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }
            } else {
                $validate = new \app\admin\validate\Doc();
                if (!$validate->check($post)) {
                    $this->error($validate->getError());
                }
                Db::startTrans();
                try {
                    Db::name('infoview')->where(['id' => $post['info_id']])->update(['info' => $this->request->param('info', '', null)]);
                    unset($post['info']);

                    Db::name('doc')->where(['id' => $post['id']])->update($post);
                    $this->wx_msg('文档编辑操作', $post['id']);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            $docinfo = Db::name('doc')->where(['id' => $post['id']])->find();
            $project_user = $this->project_user;
            if ($project_user['user_type']!=1 && ($docinfo['doc_view'] == 1 && ($docinfo['uid'] != session('uid')) )) {
                $this->assign('info', '不是文档所有者不能查看或修改');
                return view('docerror');
            } else {
                $this->assign('doc_type', config('param.doc_type'));
                $this->assign('id', input('param.id'));
                $info = Db::name('doc')
                    ->where('id', input('param.id'))
                    ->find();
                $info['c_time'] == 0 ? $info['c_time'] = '' : $info['c_time'] = date('Y-m-d', $info['c_time']);
                $info['info'] = Db::name('infoview')
                    ->where('id', $info['info_id'])
                    ->value('info');
                $doc_type = config('param.doc_type');
                $info['doc_type'] = $doc_type[$info['doc_type']];
                //dump($info);
                $this->assign('info', $info);

                return view('docwork');
            }

        }

    }
    public function doceditform()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            //单个字段修改
            if (count($post) == 3) {
                foreach ($post as $key => $info) {
                    if ($key == 'w_time') {
                        $this->error('实际时间不能修改');
                    }
                }
                Db::startTrans();
                try {
                    unset($post['editormd-html-code']);
                    Db::name('doc')->where(['id' => $post['id']])->update($post);
                    $this->wx_msg('文档编辑操作', $post['id']);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }

            } else {
                $validate = new \app\admin\validate\Doc();
                if (!$validate->check($post)) {
                    $this->error($validate->getError());
                }
                Db::startTrans();
                try {
                    Db::name('infoview')->where(['id' => $post['info_id']])->update(['info' => $this->request->param('info', '', null)]);
                    unset($post['info']);
                    $post['c_time'] = time();
                    unset($post['editormd-html-code']);
                    Db::name('doc')->where(['id' => $post['id']])->update($post);
                    $this->wx_msg('文档编辑操作', $post['id']);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        } else {
            $this->assign('doc_view', config('param.doc_view'));
            $this->assign('doc_type', config('param.doc_type'));
            $info = Db::name('doc')
                ->where('id', input('param.id'))
                ->find();
            $info['c_time'] == 0 ? $info['c_time'] = '' : $info['c_time'] = date('Y-m-d', $info['c_time']);
            $info['info'] = Db::name('infoview')
                ->where('id', $info['info_id'])
                ->value('info');
            $this->assign('info', $info);
            return view('docadd');
        }
    }
}